package com.sky.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.DishDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.lang.reflect.Array;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

//    分页
    @Override
    public PageResult list(SetmealPageQueryDTO spage) {
        // 设置分页参数
        PageHelper.startPage(spage.getPage(),spage.getPageSize());

        // 执行分页查询
        List<SetmealVO> empList = setmealMapper.list(spage);

        for (SetmealVO s : empList) {
            Long id = s.getId();
            Long categoryId = setmealMapper.selectById(id).getCategoryId();
            String name = categoryMapper.selectById(categoryId).getName();
            s.setCategoryName(name);
        }

        // 获取分页结果
        Page<SetmealVO> p = (Page<SetmealVO>) empList;
        //封装PageBean
        PageResult pageResult = new PageResult(p.getTotal(), p.getResult());
        return pageResult;
    }

//    修改套餐
    @Transactional
    @CacheEvict(cacheNames = "setmeal_category", key = "#setmealDTO.categoryId")
    public void updateDish(SetmealDTO setmealDTO){
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        log.info("修改菜品 dish{}",setmeal.toString());

        //根据id 查询 category_id
        Setmeal setmeal1 = setmealMapper.selectById(setmealDTO.getId());
        Long setmealId = setmeal1.getCategoryId();
        setmeal.setCategoryId(setmealId);
        //修改 dish
        setmealMapper.updateById(setmeal);

        //修改 口味
        for (SetmealDish flavor : setmealDTO.getSetmealDishes()) {
            //先删除dish_flavor中的对应数据
            setmealDishMapper.deletById(flavor.getSetmealId());
        }
        for (SetmealDish flavor : setmealDTO.getSetmealDishes()) {
            //在添加口味
            setmealDishMapper.saveSetmealDish(flavor);
        }
    }


//    套餐起售、停售
    @Override
    public Integer updateStatus(Long id, Integer status) {
        Integer integer = setmealMapper.updateStatus(id, status);

        return integer;
    }

//    批量删除套餐
    @Transactional
    public void removeById(Long[] ids){
        //删除 套餐
        for (Long id : ids) {
            log.info("当前删除的菜品 ID ：{}",id);
            setmealMapper.removeById(id);
        }
        //s删除 菜品
        for (Long id : ids) {
            log.info("当前删除的菜品 ID ：{}",id);
            setmealDishMapper.deletById(id);
        }
    }


    //    新增套餐
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        setmealDTO.setStatus(1);
        log.info(setmealDTO.toString());

        setmealMapper.save(setmealDTO);

        Long setmealId = setmealDTO.getId();   //套餐id
        log.info("setmealDTO:{}",setmealId.toString());

        List<SetmealDish> flavors = setmealDTO.getSetmealDishes();
        //保存套餐数据到菜品口味表setmeal_dish
        for (SetmealDish f : flavors) {
            f.setSetmealId(setmealId);
            // 默认份数
            f.setCopies(40);
            setmealDishMapper.saveSetmealDish(f);
        }
    }

//    根据id查询套餐
    @Override
    public SetmealVO selectById(Long id) {
        //获取Dish表数据
        Setmeal setmeal = setmealMapper.selectById(id);
        log.info("根据id查询菜品,{}",setmeal.toString());
        //获取套餐的菜品表数据
        List<SetmealDish> dishFlavor = setmealDishMapper.getSetmealDish(id);
        log.info("套餐数据:{}",dishFlavor.toString());

        //获取
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);

        setmealVO.setSetmealDishes(dishFlavor);
        log.info("根据id查询菜品,dishVO:{}",setmealVO);

        return setmealVO;
    }
//  根据分类id查询套餐
    @Override
    @Cacheable(cacheNames = "setmeal_category", key = "#id")
    public List<Setmeal> selectByCateId(Long id) {
        List<Setmeal> setmealList = setmealMapper.selectByCateId(id);
        List<Setmeal> collect = setmealList.stream().filter(s -> s.getStatus() == 1).collect(Collectors.toList());
        return collect;
    }

//    根据套餐id查询包含的菜品
    @Override
    public List<DishItemVO> selectCopies(Long id) {
        List<SetmealDish> setmealDishes = setmealDishMapper.getSetmealDish(id);
        List<Long> dishIds = setmealDishes.stream().map(SetmealDish::getDishId).collect(Collectors.toList());
        Map<Long,Dish> dishMap = dishMapper.selectByIds(dishIds)
                .stream().collect(Collectors.toMap(Dish::getId, dish -> dish));

        return setmealDishes.stream() .map(item -> {
            Dish dish = dishMap.get(item.getDishId());
            return DishItemVO.builder()
            .copies(item.getCopies())
            .description(dish.getDescription())
            .image(dish.getImage ())
            .name(dish.getName ())
            .build();
        }).collect(Collectors.toList());
    }


}
